import Vue from 'vue'
import Router from 'vue-router'

import departmentsRouter from './modules/departments'
import employeesRouter from './modules/employees'
import permissionRouter from './modules/permission'
import settingRouter from './modules/setting'
import approvals from './modules/approvals'
import attendances from './modules/attendances'
import salarys from './modules/salarys'
import social from './modules/social'
import user from './modules/user'

Vue.use(Router)

import Layout from '@/layout'

// 静态路由，没有权限的
export const staticstateRoutes = [
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },
  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },
  {
    path: '/',
    component: Layout,
    name: 'dashboard',
    redirect: '/dashboard',
    children: [{
      path: 'dashboard',
      name: 'dashboard',
      component: () => import('@/views/dashboard/index'),
      meta: { title: 'Dashboard', icon: 'dashboard' }
    },
    {
      path: 'import',
      name: 'import',
      hidden: true,
      component: () => import('@/views/import')
    }
    ]
  }
  // { path: '*', redirect: '/404', hidden: true }
]
// 动态路由，有权限的
export const dynamicRouter = [
  departmentsRouter,
  employeesRouter,
  permissionRouter,
  settingRouter,
  approvals,
  attendances,
  salarys,
  social,
  user
]
const createRouter = () => new Router({
  // mode: 'history', // require service support
  mode: 'history',
  // 基础地址
  base: 'hr',
  // 每次跳转页面自动置顶
  scrollBehavior: () => ({ y: 0 }),
  // 这样写所有人都可以访问
  // routes: [...staticstateRoutes, ...dynamicRouter]

  // 只写静态路由
  routes: staticstateRoutes
})

// 重置路由
// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

const router = createRouter()

export default router
